﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace yaai
{
    public partial class Form1 : Form
    {

        public List<WordList> lists = new List<WordList>();

        String selected;

        List<String> prevInsults = new List<string>();

        int currentInsult;

        int currentMode;

        int maxInsults;

        WordList nouns = new WordList("nouns");

        WordList adjectives = new WordList("adjectives");

        WordList descriptives = new WordList("descriptives");

        WordList measures = new WordList("measures");

        WordList verbs = new WordList("verbs");

        WordList roles = new WordList("roles");

        WordList numbers = new WordList("numbers");

        WordList locations = new WordList("locations");

        WordList activities = new WordList("activities");

        //MODES TEST!
        List<Mode> modes = new List<Mode>();

        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        public void Form1_Load(object sender, EventArgs e)
        {

            Console.WriteLine("LOAD");

            currentInsult = 0;

            currentMode = 1;

            selected = "";

            /*
            lists.Add(nouns);
            lists.Add(adjectives);
            lists.Add(descriptives);
            lists.Add(measures);
            lists.Add(verbs);
            lists.Add(locations);
            lists.Add(activities);
            lists.Add(numbers);
            lists.Add(roles);
            */

            readAllFilesInListsFolder();

            readModesFolder();

            addListsToListBoxes();
            addModesToListBoxes();
        }


        private WordList findMatchingWordList(String name)
        {
            foreach (WordList w in lists)
            {
                if (name == w.getName())
                {
                    return w;
                }
            }
            return null;
        }

        private Mode findMatchingMode(String name)
        {
            foreach (Mode m in modes)
            {
                if (name == m.getName())
                {
                    return m;
                }
            }
            return null;
        }


        private void addListsToListBoxes()
        {
            foreach (WordList l in lists)
            {
                listBoxLists.Items.Add(l.getName());
            }
        }

        private void addModesToListBoxes()
        {
            foreach (Mode m in modes)
            {
                listBoxModes.Items.Add(m.getName());
                cmb_selectedMode.Items.Add(m.getName());
            }
        }


        private void readAllFilesInListsFolder()
        {
            DirectoryInfo di = new DirectoryInfo("../../lists");
            FileInfo[] rgFiles = di.GetFiles("*.txt");
            foreach (FileInfo fi in rgFiles)
            {
                WordList wl = new WordList(fi.Name.Replace(fi.Extension, ""), (fi.FullName));
                wl.readFromFile();
                lists.Add(wl);
            }
        }

        private void readModesFolder()
        {
            DirectoryInfo di = new DirectoryInfo("../../modes");
            FileInfo[] rgFiles = di.GetFiles("*.txt");
            foreach (FileInfo fi in rgFiles)
            {
                using (StreamReader sr = new StreamReader(fi.FullName))
                {
                    String line;
                    String[] words;
                    List<simpleGrunka> list = new List<simpleGrunka>();
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line == "" || line == " " || line == "\n")
                            continue;

                        words = line.Split(' ');
                        foreach (String w in words)
                        {
                            if (w.Contains("<") && w.Contains(">"))
                            {
                                //its a list!
                                list.Add(new simpleGrunka(findMatchingWordList(w.Replace(">", "").Replace("<", ""))));
                            }
                            else
                            {
                                 list.Add(new simpleGrunka(w));
                            }
                        }
                        Mode m = new Mode(fi.Name.Replace(fi.Extension, ""), "", list, (fi.FullName));
                        modes.Add(m);
                    }
                }
            }
        }

        private void btn_insult_Click(object sender, EventArgs e)
        {
            //generateInsult();
            generateInsult2();
        }


        private void generateInsult2()
        {
            String result = "";


            Mode m = findMatchingMode(cmb_selectedMode.SelectedItem.ToString());

            result = m.generateInsult();

            txt_insult.Text = result;

            prevInsults.Add(result);

            comboBoxPrev.Items.Add(result);
            comboBoxPrev.SelectedItem = result;

            maxInsults++;
            currentInsult = maxInsults;

            lblCounter.Text = "" + currentInsult + "/" + maxInsults;

            if (maxInsults > 1)
            {
                btnLeft.Enabled = true;
            }
            btnRight.Enabled = false;
        }

        private void generateInsult()
        {
            String result = "";

            if (currentMode == 1)
            {
                result = "You " + descriptives.getRandomWord() + " " + measures.getRandomWord() + " of " + adjectives.getRandomWord() + " " + nouns.getRandomWord() + "!";
            }
            else if (currentMode == 2)
            {
                String role = roles.getRandomWord();
                if (role.Last() == 's')
                {
                    role = role.Remove(role.Count()-1);
                }
                result = "Your mother was a " + descriptives.getRandomWord() + " " + role + " who " + activities.getRandomWord() + " " + locations.getRandomWord() + "!";
            }
            else if (currentMode == 3)
            {
                result = "May " + numbers.getRandomWord() + " " + descriptives.getRandomWord() + " " + roles.getRandomWord() + " " + verbs.getRandomWord() + " " + locations.getRandomWord() + "!" ;
            }

            txt_insult.Text = result;

            prevInsults.Add(result);

            comboBoxPrev.Items.Add(result);
            comboBoxPrev.SelectedItem = result;

            maxInsults++;
            currentInsult = maxInsults;
           
            lblCounter.Text = "" + currentInsult + "/" + maxInsults;

            if (maxInsults > 1)
            {
                btnLeft.Enabled = true;
            }
            btnRight.Enabled = false;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btn_remove_word.Enabled = false;
            btn_add_word.Enabled = false;
            try
            {
                String selec = listBoxLists.SelectedItem.ToString();

                if (selec != null)
                {
                    btn_add_word.Enabled = true;
                    btn_remove_list.Enabled = true;
                }
                else
                {
                    btn_remove_list.Enabled = false;
                }

                if (!(selec == selected))
                {
                    foreach (WordList w in lists)
                    {
                        if (selec.Contains(w.getName()))
                        {
                            selected = w.getName();
                            listBoxWords.Items.Clear();
                            foreach (String s in w.getWordList())
                                listBoxWords.Items.Add(s);
                        }
                    }
                }
            }
            catch(Exception err){
                Console.Out.WriteLine(err.Message);
            }
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            btnRight.Enabled = true;
            currentInsult--;
            
            if (currentInsult == 1)
                btnLeft.Enabled = false;
            prevClick();
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            btnLeft.Enabled = true;
            currentInsult++;

            if (currentInsult == maxInsults)
                btnRight.Enabled = false;
            prevClick();
        }

        private void prevClick()
        {
            txt_insult.Text = prevInsults.ElementAt(currentInsult - 1);
            comboBoxPrev.SelectedItem = prevInsults.ElementAt(currentInsult - 1);
            lblCounter.Text = "" + currentInsult + "/" + maxInsults;
        }

        private void radioButton_Changed(Object sender, EventArgs e)
        {
            modeChanged((RadioButton)sender, e);
        }

        private void modeChanged(RadioButton sender, EventArgs e)
        {
            int.TryParse(sender.Text, out currentMode);
        }

        private void btn_three_add_Click(object sender, EventArgs e)
        {
            if (listBoxLists.SelectedItem != null)
            {
                this.SuspendLayout();
                addWord add = new addWord();
                add.ShowDialog();

                string word = add.word;
                int profanityLevel = add.profanityLevel;


                WordList selected = findMatchingWordList(listBoxLists.SelectedItem.ToString());

                selected.addWord(word);
                listBoxWords.Items.Add(word);
                listBoxWords.Refresh();
            }
        }

        private void listBoxWords_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxWords.SelectedItem != null)
                btn_remove_word.Enabled = true;
            else
                btn_remove_word.Enabled = false;
        }

        private void btn_remove_list_Click(object sender, EventArgs e)
        {
            String selected = listBoxLists.SelectedItem.ToString();

            listBoxLists.Items.Remove((String)listBoxLists.SelectedItem);

            lists.Remove(findMatchingWordList(selected));
            //TODO: Delete the file? Move to new folder?
        }

        private void btn_remove_word_Click(object sender, EventArgs e)
        {
            String selectedWord = listBoxWords.SelectedItem.ToString();
            String selectedList = listBoxLists.SelectedItem.ToString();
            WordList selec = findMatchingWordList(selectedList);

            selec.removeWord(selectedWord);
            listBoxWords.Items.Remove(selectedWord);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.SuspendLayout();
            addMode add = new addMode(lists);
            add.ShowDialog();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            writeAllListsToFile();
        }

        private void writeAllListsToFile()
        {
            foreach (WordList l in lists)
            {
                if (l.changed)
                {
                    l.writeAllToFile();
                }
            }
        }

        private void btn_add_list_Click(object sender, EventArgs e)
        {
            addList add = new addList(this);
            add.ShowDialog();
            listBoxLists.Items.Clear();
            addListsToListBoxes();
        }

        private void listBoxModes_SelectedIndexChanged(object sender, EventArgs e)
        {
            Mode m = findMatchingMode(listBoxModes.SelectedItem.ToString());
            lbl_descr_setter.Text = m.getDescription();
            lbl_name_setter.Text = m.getName();
            txt_mode_descriptor.Text = m.generateDescriptor();
        }
    }
}
